package com.goods.business.service.imp;

import com.goods.business.mapper.InStockInfoMapper;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private ProductMapper productMapper;

    //加载表格数据
    @Override
    public PageVO<InStockVO> loadTableData(Integer pageNum, Integer pageSize, Integer status, InStockVO inStockVO) {
        Example example = new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();

        //获取条件
        //根据类型
        @NotNull(message = "入库单类型不能为空") Integer type = inStockVO.getType();
        criteria.andEqualTo("type",type);
        //根据单号
        String inNum = inStockVO.getInNum();
        if (!StringUtils.isEmpty(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }
        //根据时间
        Date createTime = inStockVO.getCreateTime();
        criteria.andGreaterThanOrEqualTo("createTime",createTime);
        Date modified = inStockVO.getModified();
        criteria.andLessThanOrEqualTo("modified",modified);
        //根据 status
        criteria.andEqualTo("status",status);
        List<InStock> inStocks = inStockMapper.selectByExample(example);
        List<InStockVO> inStockVOList = inStocks.stream().map(inStock -> {
            InStockVO inStockVO1 = new InStockVO();
            BeanUtils.copyProperties(inStock,inStockVO1);
            return inStockVO1;
        }).collect(Collectors.toList());
        List<InStockVO> page = ListPageUtils.page(inStockVOList, pageSize, pageNum);
        if (page != null){
            PageVO<InStockVO> inStockVOPageVO = new PageVO<>(inStockVOList.size(), page);
            return inStockVOPageVO;
        }else {
            PageVO<InStockVO> inStockVOPageVO = new PageVO<>(inStockVOList.size(), inStockVOList);
            return inStockVOPageVO;
        }
    }

    //明细
    @Override
    public List<InStockDetailVO> detail(Long id, Integer pageNum) {
        List<InStockDetailVO> inStockDetailVOList = new ArrayList<>();
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        //获取biz_in_stock对象
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        Long supplierId = inStock.getSupplierId();

        //获取biz_supplier对象
        Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
        //封装数据   北京  昌平  张三  电话
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);

        //获取biz_in_stock_info对象
        String inNum = inStock.getInNum();
        Example example1 =new Example(InStockInfo.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("inNum",inNum);
        InStockInfo inStockInfo = inStockInfoMapper.selectOneByExample(example1);
        //封装数据   innum
        inStockDetailVO.setInNum(inStockInfo.getInNum());

        //获取biz_product对象
        String pNum = inStockInfo.getPNum();
        Example example2 =new Example(InStockInfo.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria1.andEqualTo("pNum",pNum);
        Product product = productMapper.selectOneByExample(example2);
        //封装数据
        inStockDetailVO.setStatus(product.getStatus());




    }
}
